package com.test.jbpm.web.action;


import java.io.IOException;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.test.jbpm.CheckboxTreeMaker;
import com.test.jbpm.domain.Dept;
import com.test.jbpm.service.DeptService;



@Controller
@Scope("prototype")
public class DeptAction extends BaseAction {

	private DeptService deptService;
	private Dept dept;
	private CheckboxTreeMaker checkb;
	
	public Dept getDept() {
		return dept;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}

	public DeptService getDeptService() {
		return deptService;
	}

	@Autowired
	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}
	
	public String index() throws Exception {
		return SUCCESS;
	}

//	public String list() throws Exception {
//		List<Dept> deptList = deptService.findDeptList();
//		String jsonString = JSONArray.fromObject(deptList).toString();
//		getResponse().setContentType("text/html;charset=utf-8");
//		getResponse().getWriter().print(jsonString);		
//		System.out.println(ajaxOut(deptList));
//		return ajaxOut(deptList);
//	}
	public String list(){
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/xml; charset=utf-8");
		dept = new Dept();		
		dept.setId(1);
		Dept oo =	deptService.findFirstLevelOfTree(dept);  
		Set<Dept> c = oo.getChildren();
		checkb = new CheckboxTreeMaker(oo,c);
		String hh = checkb.make(); 
		System.out.println("ppppppppppp"+hh);
		try {
			response.getWriter().print(hh);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
